package com.samebuilding.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.samebuilding.domain.Activity;
import com.samebuilding.service.ActivityService;
import com.samebuilding.util.PaginationUtil;

public class AllActivityAction extends DispatchAction {

	private ActivityService activityService;
	
	public ActivityService getActivityService() {
		return activityService;
	}

	public void setActivityService(ActivityService activityService) {
		this.activityService = activityService;
	}

	public ActionForward allActivity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		int limit = 10;
		
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = NumberUtils.createInteger(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Activity> list = this.activityService.findAll(start, limit);
		
		int count = this.activityService.findCountAll();
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		return mapping.findForward("allActivity");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		int activityId = NumberUtils.createInteger(request.getParameter("activityId"));
		
		this.activityService.deleteActivity(activityId);
		
		
		response.sendRedirect("allActivity.do?method=allActivity");
		
		return null;
	}
}
